package org.zyx.item.service;

import org.springframework.stereotype.Service;
import org.zyx.item.entity.SpecGroup;
import org.zyx.item.entity.SpecParam;
import org.zyx.item.mapper.SpecGroupMapper;
import org.zyx.item.mapper.SpecParamMapper;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: SwordMaple
 * @Date: 2020/9/6  20:14
 * @Description: 规格参数组及规格参数合并服务
 * @Version: 1.0
 */
@Service
public class SpecificationService {

    @Resource
    private SpecGroupMapper specGroupMapper;
    @Resource
    private SpecParamMapper specParamMapper;

    /**
     * 根据分类id查询查询参数组
     * @param cid
     * @return
     */
    public List<SpecGroup> findGroupsByCid(Long cid) {

        SpecGroup recode = new SpecGroup();
        recode.setCid(cid);//设置查询条件
        List<SpecGroup> specGroups = specGroupMapper.select(recode);
        return specGroups;
    }

    /**
     *  根据条件查询参数
     * @param gid  参数组
     * @param cid   商品分类
     * @param generic 是否是通用
     * @param searching 是否是查询字段
     * @return
     */
    public List<SpecParam> findParams(Long gid, Long cid, Boolean generic, Boolean searching) {
        SpecParam recode = new SpecParam();
        recode.setGroupId(gid);//设置查询条件
        recode.setCid(cid);
        recode.setGeneric(generic);
        recode.setSearching(searching);
        List<SpecParam> specParams = specParamMapper.select(recode);
        return specParams;
    }

    /**
     * 插入参数分组
     * @param specGroup
     * @return
     */
    public Boolean addSpecGroup(SpecGroup specGroup) {
        if(specGroupMapper.insert(specGroup) > 0){
            return true;
        }
        return false;
    }
    /**
     * 更新参数分组
     * @param specGroup
     * @return
     */
    public boolean updSpecGroup(SpecGroup specGroup) {
        if(specGroupMapper.updateByPrimaryKey(specGroup) > 0){
            return true;
        }
        return false;
    }

    /**
     * 根据id删除参数分组
     * @param id 组id
     * @return
     */
    public boolean deleteSpecGroupById(Long id) {
        if(specGroupMapper.deleteByPrimaryKey(id) > 0){
            return true;
        }
        return false;
    }

    /**
     * 添加参数
     * @param specParam
     * @return
     */
    public boolean addParam(SpecParam specParam) {
        if(specParamMapper.insert(specParam) > 0){
            return true;
        }
        return false;
    }

    /**
     * 更新参数
     * @param specParam
     * @return
     */
    public boolean updParam(SpecParam specParam) {
        if(specParamMapper.updateByPrimaryKey(specParam) > 0){
            return true;
        }
        return false;
    }

    /**
     * 根据id删除参数
     * @param id
     * @return
     */
    public boolean deleteSpecParamById(Long id) {
        if(specParamMapper.deleteByPrimaryKey(id) > 0){
            return true;
        }
        return false;
    }

    /**
     * 通过cid查找分类组级参数
     * @param cid
     * @return
     */
    public List<SpecGroup> findGroupsWithParam(Long cid) {

        //1.查找组
        List<SpecGroup> groups = this.findGroupsByCid(cid);
        //2.添加参数
        groups.forEach(group -> {
            List<SpecParam> params = this.findParams(group.getId(), null, null, null);
            group.setParams(params);
        });

        return groups;
    }
}
